package cc.bluemc.modules.adminUser.service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cc.bluemc.modules.adminUser.entity.AdminUser;
import cc.bluemc.modules.adminUser.entity.Role;
import cc.bluemc.platform.jdbctemplatetool.JdbcTemplateTool;
import cc.bluemc.platform.jdbctemplatetool.exception.NoColumnAnnotationFoundException;
import cc.bluemc.platform.jdbctemplatetool.exception.NoIdAnnotationFoundException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class RoleService {
	
	private Log log = LogFactory.getLog(this.getClass());
	@Autowired
	private JdbcTemplateTool jdbcTemplateTool;
	
	
	@Transactional(readOnly=false)
	public int save(Role entiy) throws Exception {
			return jdbcTemplateTool.save(entiy,"id");
	}
	
	public Role getBean(int id) throws NoIdAnnotationFoundException, NoColumnAnnotationFoundException{
		return jdbcTemplateTool.get(Role.class, id);
	}
	/**
	 * 查询用户可用角色
	 * @param user
	 * @return
	 */
	public List<Role> getUserRoles(AdminUser user){
		return getUserRoles(user.getId());
	}
	/**
	 * 查询用户可用角色
	 * @param userId
	 * @return
	 */
	public List<Role> getUserRoles(Integer userId){
		String sql ="SELECT * FROM t_role r LEFT JOIN t_adminuser_role ar ON r.id= ar.roleId WHERE ar.userId=? and r.usable=1";
		return jdbcTemplateTool.list(sql, new Object[]{userId}, Role.class);
	}
	/**
	 * 查询用户可用角色Id
	 * @param user
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List getUserRoles2(AdminUser user){
		String sql ="SELECT ar.roleId FROM t_adminuser_role ar LEFT JOIN t_role r ON r.id= ar.roleId WHERE ar.userId=? and r.usable=1";
		 List<Map<String, Object>> list = jdbcTemplateTool.getJdbcTemplate().queryForList(sql,new Object[]{user.getId()});
		 List returnList = new ArrayList();
		 if(null!=list){
			 for(Map map:list){
				 returnList.add(map.get("roleId"));
			 }
		 }
		 return returnList;
	}
	/**
	 * 查询所有可用角色
	 * @return
	 */
	public List<Role> getUserableRoles(){
		String sql ="SELECT * FROM t_role r where r.usable=1";
		return jdbcTemplateTool.list(sql, new Object[]{}, Role.class);
	}
	/**
	 * 查询所有角色
	 * @return
	 */
	public List<Role> getAllRoles(){
		String sql ="SELECT * FROM t_role r ";
		return jdbcTemplateTool.list(sql, new Object[]{}, Role.class);
	}
	@Transactional(readOnly=false)
	public void deleBean(Role role) throws Exception{
		jdbcTemplateTool.delete(role);
	}
	@Transactional(readOnly=false)
	public void updateBean(Role role) throws Exception{
		jdbcTemplateTool.update(role);
	}

	/**
	 * 设置角色权限
	 * @param roleId
	 * @param permis
	 */
	@Transactional(readOnly=false)
	public void setPermi(final Integer roleId,final Integer [] permis){
		JdbcTemplate jdbcTemplate = jdbcTemplateTool.getJdbcTemplate();
		jdbcTemplate.update("DELETE FROM t_role_perm WHERE roleId=?", new Object[]{roleId});
		if(null!=permis){
			jdbcTemplate.batchUpdate("insert into t_role_perm (roleId, permId) values (?, ?)", new BatchPreparedStatementSetter(){
				@Override
				public int getBatchSize() {
					return permis.length;
				}
				@Override
				public void setValues(PreparedStatement arg0, int arg1)
				throws SQLException {
					arg0.setInt(1, roleId);
					arg0.setInt(2, permis[arg1]);
				}
				
			});
		}
	}
}
